package game.module.sss.procesor;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.qzmj.QzmjConstants;
import game.module.sss.bean.SssRound;
import game.module.sss.logic.SssDataManager;
import game.module.user.bean.PlayerBean;
import game.module.user.logic.RoomDataManager;
import lion.common.ArrayUtility;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageSss.C2SSssBackRoom;
import lua.LuaMessageSss.IntList;
import lua.LuaMessageSss.S2CSssBackRoom;
import lua.LuaMessageSss.SSSMemberInfo;
import lua.LuaMessageSss.SssCardArray;
@MsgCodeAnn(msgcode=C2SSssBackRoom.id,accessLimit=1000)
public class SssBackRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(SssBackRoomProcessor.class);
	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("sss back to room,playerId={}", playerId);
		//房间是否存在
		SssRound sssround = SssDataManager.getInstance().getNnRoom(playerId);
		if(sssround ==null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;	
		}
		//do
		S2CSssBackRoom respMsg = new S2CSssBackRoom();
		respMsg.room_id = sssround.getRoomUuid();
		respMsg.round_count = sssround.getRoundCount();
		respMsg.anti_cheet = sssround.getAntiCheet();
		respMsg.mapai = sssround.getMapai();
		respMsg.daqiang = sssround.getDaqiang();
		respMsg.sunzi_a = sssround.getSunzi_a();
		respMsg.baodao = sssround.getBaodao();
		respMsg.is_jindou = sssround.getIs_jindou();
	//	respMsg.jindou_type = sssround.getJindou_type();
		respMsg.current_round = sssround.getCurrentRound();
		respMsg.jindounum = sssround.getJindouDi();
		respMsg.members = new SSSMemberInfo[sssround.getPlayerIds().size()];
		int i = 0;
		List<Integer> playerIdList = sssround.getPlayerIds();
		for(Integer aPlayerId : playerIdList) {
			PlayerBean poc = RoomDataManager.getInstance().getPlayerBean(aPlayerId);
			respMsg.members[i++] = new SSSMemberInfo(aPlayerId, poc.getHeadimg(), 1, poc.getSex().shortValue(),
					0,poc.getName());
		}
		respMsg.create_time = (int)(sssround.getCreate_time()/1000);
	//	long m_lScore[] = sssround.m_lGameScore;
		if(sssround.m_bToltaSocre!=null)
		    respMsg.score =sssround.m_bToltaSocre;
		respMsg.finishedplayer = null;
		
		if(sssround.getCards()!=null && !sssround.getCards().isEmpty())
		    respMsg.my_cards = sssround.getCards().get(playerIdList.indexOf(playerId));
		
		if(sssround.getPeiPaiFinish()!=null && sssround.getPeiPaiFinish().size()>0) {
			respMsg.finishedplayer = new int[sssround.getPeiPaiFinish().size()];
			int j=0;
			for (Map.Entry<Integer, byte[]> entry : sssround.getPeiPaiFinish().entrySet()) {
        		
				respMsg.finishedplayer[j++] =  entry.getKey();
        	}
		}
		
		if(sssround.getPeiPaiFinish()!=null && sssround.getPeiPaiFinish().size() == playerIdList.size()) {
			respMsg.card_all = new SssCardArray[playerIdList.size()];
			for(int k =0;k<playerIdList.size();k++) {
				int playerid = sssround.getPlayerIds().get(k);
				respMsg.card_all[k] = new SssCardArray();
				respMsg.card_all[k].pai = sssround.getPeiPaiFinish().get(playerid);
			}
		}
		
		if (sssround.m_bCompareResult != null && sssround.m_bCompareResult.length > 0) {
			respMsg.m_bCompareResult = new IntList[sssround.m_bCompareResult.length];

			for (int j = 0; j < sssround.m_bCompareResult.length; j++) {
				respMsg.m_bCompareResult[j] = new IntList();
				respMsg.m_bCompareResult[j].pai = sssround.m_bCompareResult[j];
			}
		}
		if(sssround.m_bToltalWinDaoShu!=null)
		    respMsg.m_bToltalWinDaoShu = sssround.m_bToltalWinDaoShu;
		if(sssround.m_lGameScore!=null)
		    respMsg.m_lGameScore =  ArrayUtility.long2Int(sssround.m_lGameScore);
		if(sssround.m_bSpecialTypeTable!=null)
		    respMsg.use_special = sssround.m_bSpecialTypeTable;
		if(sssround.getCreateVotedissTime()>0) {
			respMsg.votedissPlayer = sssround.getVoteDissPlayer();
			byte vote[] = new byte[sssround.getPlayerIds().size()];
			boolean dissvote[] = sssround.getVotediss();
			int k =0;
			for(boolean aVote:dissvote) {
				if(aVote)
					vote[k]=1;
				k++;
			}
			respMsg.votedissstate =vote;
			respMsg.votedisstime = (int)(QzmjConstants.AUTODISSTIME-((System.currentTimeMillis()-sssround.getCreateVotedissTime())/1000));
			logger.info("last time dissmiss sssroom time = {}",respMsg.votedisstime);
		}
		respMsg.player_count = sssround.getPlayer_count();
		respMsg.add_color = sssround.getAdd_color();
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
	
		
	}
    
}
